﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine.EventSystems;
using System.Collections;

/// <summary>
/// The purpose of this component is to fix a bug in how the unity ui text editor selector is created.
/// Because we have no control over the creation, we have to fix it up after it has been instantiated..
/// </summary>
public class PGEditFixer : MonoBehaviour, ISelectHandler
{
    public void OnSelect(BaseEventData eventData)
    {
        StartCoroutine(FixBug());
        // This only needs to be ran once..
    }

    public IEnumerator FixBug()
    {
        yield return new WaitForEndOfFrame();

        var transforms = gameObject.GetComponentsInChildren<RectTransform>().ToList();
        var icTransform = transforms[1];
        icTransform.anchorMin = new Vector2(0f, 0f);
        icTransform.anchorMax = new Vector2(1f, 1f);
        icTransform.position = gameObject.transform.position;
        icTransform.offsetMin = new Vector2(16f, -5f);
        icTransform.offsetMax = new Vector2(0f, 0f);

        Destroy(this);
        yield break;
    }
}

